// constants for scripts
var popupTitle = null;
var popupUrl = null;
var popupDialog = null;
	
var activePopup = null;
var activeTrigger = null;

function HidePopups(event) {
	var e = event || window.event;
	var el = e.target != null ? e.target : e.srcElement;
	if(el.className.substr(0,7).toUpperCase() == "HITAREA") return false;
	$(".popup").each(function(){ $(this).hide(); });
	$("body").unbind('mouseup', HidePopups).unbind('keyup', HidePopups);
	activePopup = null;
	activeTrigger = null;
	return false;
};

function popup(title, url, h, w) {
	popupTitle = title;
	popupUrl = url;
	if ($("#dialog-form").length == 0) {
		$('<div />', {id: 'dialog-form'}).appendTo('body');
	}
	$("#dialog-form")
		.load(popupUrl, function(responseText, textStatus, XMLHttpRequest) {
			$('form', popupDialog).submit(popupFormSubmit);
		})
		.dialog({
			title: popupTitle,
			autoOpen: true,
			height: h ? h : 500,
			width: w ? w : 400,
			modal: true,
			close: function () {
				popupTitle = null;
				popupUrl = null;
				popupDialog = null;
			},
			open: function () {
				popupDialog = $(this);
			}
		});
}

popupFormSubmit = function() {
	var post_iframe = $('#lil_post_iframe');
	
	// do a ajax submit if frame does not exist
	// otherwise do a real submit to iframe (for file uploads)
	if (typeof $(post_iframe).get(0) == 'undefined') {
		$.post(
			popupUrl,
			$('form', popupDialog).serialize(),
			function(data) {
				$(popupDialog).html(data);
				$('form', popupDialog).submit(popupFormSubmit);
			}
		);
	} else {
		var to = window.setTimeout('realPopupSubmit()', 10);
	}
	return false;
}

function realPopupSubmit() {
	// fetch form
	var post_form = $('form', popupDialog);
	
	if (post_form.length > 0) {
		// check if hidden iframe exists
		var post_iframe = $('#lil_post_iframe');
		if ($(post_iframe).length > 0) {
			// add hidden fields
			$('<input/>', {type:'hidden', name:'lil_submit', value:'dialog'}).attr('class', 'lil_submit').appendTo(post_form);
			
			// modify form to post to iframe
			$(post_form).unbind('submit', popupFormSubmit).attr('target', 'lil_post_iframe').submit();
			
			$(post_iframe).load(function() {
				var data = $(post_iframe).contents().find('body').html();
				var contents = jQuery.parseJSON(data);
				if (typeof contents['data'] != 'undefined') {
					$(popupDialog).html(decodeURIComponent(contents['data']));
					
					// check if popup is still open
					if (typeof $(popupDialog).get(0) != 'undefined') {
						$('form', popupDialog).submit(popupFormSubmit);
					}
				}
			});
		}
	}
}
	
$(document).ready(function(){ 

	$(".popup_link").each(function(){
		$(this).click(function() {
			var pos = $(this).position();
			
			activePopup = $("."+this.id);
			activeTrigger = this;
	        //$("."+this.id).css("left", pos.left).css("top", pos.top+$(this).height()).show();
	        
	        // jqeury ui position plugin
	        $("."+this.id).show().position({
			  my: "left top",
			  at: "left bottom",
			  of: $(this),
			  offset: "0 5"
			});

	        $("body").bind('mouseup', HidePopups).bind('keyup', HidePopups);
			return false;
		});
	});
	
	$(".inplace_link").each(function(){
		$(this).click(function() {
			var inplaceElement = $('.' + $(this).attr('id'));
			if (typeof inplaceElement != 'undefined') {
		        $(this).after($(inplaceElement).show()).hide();
				$('input:first', $(inplaceElement)).focus();
			}
			
			return false;
		});
	});
	
	$('.popup').each(function(){ $(this).hide(); });
	$('.inplace').each(function(){ $(this).hide(); });
});

$(window).resize(function(){
	if (activePopup) {
		$(activePopup).position({
		  my: "left top",
		  at: "left bottom",
		  of: $(activeTrigger),
		  offset: "0 5"
		});
			
		//$(activePopup).css("left", $(activeTrigger).offset().left)
		//	.css("top", $(activeTrigger).offset().top + $(activeTrigger).height());
	}
});